package com.sudoku.utility;

import java.util.ArrayList;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.ParticleEffect;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.sudoku.Sudoku;
import com.sudoku.device.Device;
import com.sudoku.observer.Event;
import com.sudoku.observer.Subject;
import com.sudoku.sound.ConsoleAudio;
import com.sudoku.sound.IAudio;

public class Locator implements Constants {
	private static IAudio audioService;
	private static ArrayList<ParticleEffect> effects;
	private static InputMultiplexer multi;
	private static Sudoku game;
	private static FitViewport viewport;
	public static DIFFICULTY difficulty;
	public static boolean mute;
	public static Assets assets;
	public static TimedEventManager events;
	public static Device device;
	public static Subject subject;
	public static boolean gameInProgress;
	public static float adTimer;
	public static OrthographicCamera camera;
	public static float xScale, yScale, overallScale;
	public static float yOffset;
	public static float VIRTUAL_BOARD_PIXEL_SIZE;
	public static float BOX_PIXEL_SIZE;
	public static float TILE_PIXEL_SIZE;
	public static float BOARD_START_POSITION;
	public static float MAX_AD_TIME = 180;
	public static boolean showTimer, highlightTiles, showHints;

	public static void init(Sudoku g, Device d, IAudio audio, OrthographicCamera cam, FitViewport view, Subject sub) {

		effects = new ArrayList<ParticleEffect>();
		multi = new InputMultiplexer();
		game = g;
		adTimer = MAX_AD_TIME;
		device = d;
		camera = cam;
		showTimer = highlightTiles = showHints = true;
		audioService = new ConsoleAudio();
		events = new TimedEventManager();
		difficulty = DIFFICULTY.EASY;
		assets = new Assets();
		subject = sub;
		gameInProgress = false;
		assets.loadAssets();
		audioService = audio;
		viewport = view;
		resize();
	}

	public static void scheduleEvent(float time, Event event, Subject subject) {
		events.scheduleEvent(time, event, subject);
	}

	public static AssetManager getAssets() {
		return assets.getManager();
	}

	public static void win() {
	}

	public static void readSave() {
		Preferences prefs = Gdx.app.getPreferences("My Preferences");
		gameInProgress = prefs.getBoolean("gameInProgress", false);
		if (gameInProgress) {
			game.getGameScreen().readSave(prefs);
		}
		mute = prefs.getBoolean("mute", false);
		showTimer = prefs.getBoolean("showTimer", true);
		highlightTiles = prefs.getBoolean("highlightTiles", true);
		showHints = prefs.getBoolean("showHints", true);
	}

	public static void writeSave() {
		Preferences prefs = Gdx.app.getPreferences("My Preferences");
		prefs.putBoolean("mute", mute);
		prefs.putBoolean("showTimer", showTimer);
		prefs.putBoolean("highlightTiles", highlightTiles);
		prefs.putBoolean("showHints", showHints);

		game.getGameScreen().writeSave(prefs);
		prefs.flush();
	}

	public static FitViewport getViewport() {
		return viewport;
	}

	public static void setMute(boolean m) {
		mute = m;
	}

	public static Device getDevice() {
		return device;
	}

	public static InputMultiplexer getInputMultiplexer() {
		return multi;
	}

	public static IAudio getAudio() {
		return audioService;
	}

	public static ArrayList<ParticleEffect> getEffects() {
		return effects;
	}

	public static Sudoku getGame() {
		return game;
	}

	public static OrthographicCamera getCamera() {
		return camera;
	}

	public static void setCamera(OrthographicCamera camera) {
		Locator.camera = camera;
	}

	public static TimedEventManager getEvents() {
		return events;
	}

	public static void setEvents(TimedEventManager events) {
		Locator.events = events;
	}

	public static boolean isMute() {
		return mute;
	}

	public static DIFFICULTY getDifficulty() {
		return difficulty;
	}

	public static void setDifficulty(DIFFICULTY d) {
		difficulty = d;
	}

	public static void resize() {
		xScale = Gdx.graphics.getWidth() / ACTUAL_VIRTUAL_WIDTH;
		yScale = Gdx.graphics.getHeight() / ACTUAL_VIRTUAL_HEIGHT;
		yOffset = (Gdx.graphics.getHeight() - viewport.getViewportHeight()) / 2;
		overallScale = (xScale + yScale) / 2;
		VIRTUAL_BOARD_PIXEL_SIZE = ACTUAL_VIRTUAL_HEIGHT * viewport.getViewportHeight() / viewport.getWorldHeight() * SCALE;
		BOX_PIXEL_SIZE = VIRTUAL_BOARD_PIXEL_SIZE / 3;
		TILE_PIXEL_SIZE = BOX_PIXEL_SIZE / 3;
		BOARD_START_POSITION = ACTUAL_VIRTUAL_WIDTH * viewport.getViewportWidth() / viewport.getWorldWidth() - VIRTUAL_BOARD_PIXEL_SIZE;
		viewport.setCamera(camera);
	}

	public static void setShowTimer(boolean checked) {
		showTimer = checked;

	}

	public static void setHighlightTiles(boolean checked) {
		highlightTiles = checked;

	}

	public static void setShowHints(boolean checked) {
		showHints = checked;

	}

	public static void resetAdTimer() {
		adTimer = 0;

	}

}
